package com.nbsaas.boot.controller;

import com.nbsaas.boot.service.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
 * 状态控制器
 * 提供健康检查、服务注册状态管理等API
 * 
 * @author nbsaas
 */
@RestController
@RequestMapping("/api/status")
public class StatusController {

    @Autowired
    private RegistryService registryService;

    /**
     * 健康检查接口
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> health() {
        Map<String, Object> health = new HashMap<>();
        health.put("status", "UP");
        health.put("timestamp", Instant.now());
        health.put("service", "nb-cloud-hub-gateway");
        health.put("version", "1.0-SNAPSHOT");
        
        return ResponseEntity.ok(health);
    }

    /**
     * 获取应用详细信息
     */
    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> info() {
        Map<String, Object> info = new HashMap<>();
        info.put("app", "nb-cloud-hub");
        info.put("description", "Cloud Hub Gateway Application");
        info.put("version", "1.0-SNAPSHOT");
        info.put("java.version", System.getProperty("java.version"));
        info.put("spring.boot.version", org.springframework.boot.SpringBootVersion.getVersion());
        info.put("build.timestamp", Instant.now());
        
        return ResponseEntity.ok(info);
    }

    /**
     * 注册当前实例到注册中心
     */
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestParam(defaultValue = "nacos") String type) {
        try {
            registryService.registerSelf(type);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "服务注册成功");
            result.put("registryType", type);
            result.put("timestamp", Instant.now());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "服务注册失败: " + e.getMessage());
            result.put("registryType", type);
            result.put("timestamp", Instant.now());
            
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 从注册中心注销当前实例
     */
    @PostMapping("/deregister")
    public ResponseEntity<Map<String, Object>> deregister(@RequestParam(defaultValue = "nacos") String type) {
        try {
            registryService.deregisterSelf(type);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "服务注销成功");
            result.put("registryType", type);
            result.put("timestamp", Instant.now());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "服务注销失败: " + e.getMessage());
            result.put("registryType", type);
            result.put("timestamp", Instant.now());
            
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 更新服务实例状态
     */
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateStatus(
            @RequestParam String status,
            @RequestParam(defaultValue = "nacos") String type) {
        try {
            registryService.updateStatus(status, type);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "状态更新成功");
            result.put("status", status);
            result.put("registryType", type);
            result.put("timestamp", Instant.now());
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "状态更新失败: " + e.getMessage());
            result.put("status", status);
            result.put("registryType", type);
            result.put("timestamp", Instant.now());
            
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 获取当前注册状态
     */
    @GetMapping("/registry")
    public ResponseEntity<Map<String, Object>> getRegistryStatus() {
        Map<String, Object> status = registryService.getRegistryStatus();
        return ResponseEntity.ok(status);
    }
} 